#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sqlite3.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "commonality.h"
#include "client.h"
#include "sqlite.h"
int flags = -1;

int main(int argc, char *argv[])
{
	int socketfd ;
	struct sockaddr_in server_addr;
	USER user;
	
	if (argc < 3)
	{
		printf("Usage : %s <serv_ip> <serv_port>\n", argv[0]);
		exit(-1);
	}

	if ((socketfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)//创建套接字
	{
		perror("fail to socket");
		exit(-1);
	}

	bzero(&server_addr, sizeof(server_addr));

	server_addr.sin_family = PF_INET;//ipv4协议
	server_addr.sin_addr.s_addr = inet_addr(argv[1]);//ip地址
	server_addr.sin_port = htons(atoi(argv[2]));//端口
	
	if (connect(socketfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)//连接
	{
		perror("fail to connect");
		exit(-1);
	}

	int n;

	while(1)
	{
		printf("***********************************************\n");
		printf("******1:admin login  2:user login  3:quit******\n");//管理员登录   员工登录
		printf("***********************************************\n");
		printf("please choose : ");

		if(scanf("%d", &n) <= 0)
		{
			perror("scanf");
			exit(-1);
		}

		switch(n)
		{
			case 1:
				if(do_admin_login(socketfd, &user) == 0)
				goto next1;
				break;
			case 2:
				if(do_user_login(socketfd, &user) == 0)
				goto next2;
				break;
			case 3:
				close(socketfd);
				exit(0);
		}
	}
	
next1://管理员功能
	while(1){
		printf("***********************************************\n");
		printf("*1:query 2:update 3:adduser 4:deluser 5:quit*\n");//查询   修改   增加员工  删除员工    退出
		printf("***********************************************\n");
		printf("please choose : ");	
	
		if(scanf("%d", &n) <= 0){
			perror("scanf");
			exit(-1);
		}

		switch(n)
		{
			case 1:
				do_admin_query(socketfd, &user);
				break;
			case 2:
				do_admin_update(socketfd, &user);
				break;	
			case 3:
				do_admin_adduser(socketfd, &user);
				break;
			case 4:
				do_admin_deluser(socketfd, &user);
				break;	
			case 5:
				close(socketfd);
				exit(0);
		}			
	}
	
next2://员工功能
	while(1){
		printf("***********************************************\n");
		printf("1:query and update myself 2:query others 3:quit\n");//查询和修改自己    查询别人   退出
		printf("***********************************************\n");
		printf("please choose : ");
		if(scanf("%d", &n) <= 0){
			perror("scanf");
			exit(-1);
		}
	
		switch(n)
		{
			case 1:
				do_query_and_update_myself(socketfd, &user);
				break;
			case 2:
				do_query_others(socketfd, &user);
				break;	
			case 3:
				close(socketfd);
				exit(0);
		}			
	}
		
	return 0;
}

int do_admin_login(int socketfd, USER *user)
{	
	printf("input your name:");
	scanf("%s",user->name);
	printf("input your password:");
	scanf("%s",user->password);
	if(strncmp("root",user->name,4) ==0){
		user->type = ROOT_LOG;//超级用户登录
		flags = 0;
	}else{
		user->type = ADMIN_LOG;//管理员登录
		flags = 1;	
	}
	
	send(socketfd, user, sizeof(USER), 0);
	recv(socketfd, user, sizeof(USER), 0);
	//printf("user.err=%d\n",user->err);

	if(user->err){
		printf("username or password is error,please check !\n");
		return -1;
	}else{
		printf("login success !\n");
	}
	
	return 0;
}	
	
void do_admin_query(int socketfd, USER *user)
{
	int name_query_level = -1;
	INFO info;
	query_desc_t query_info;

	memset(&query_info, 0, sizeof(query_info));


	if(flags == 1)
	{
		user->type = ADMIN_QUERY;
	}
	if(flags == 0)
	{
		user->type = ADMIN_QUERY_ALL;
	}
	
	send(socketfd,user,sizeof(user),0);// query,level

//	printf("before send user%d\n",user->type);

	printf("input query name:");
	scanf("%s",info.name);
	getchar();

	if(flags == 1)//管理员查询
	{
		send(socketfd, &info, sizeof(info), 0);//send name
	
		recv(socketfd, &info, sizeof(info), 0);//接收到查询姓名的等级
		name_query_level = info.level;

		recv(socketfd,&query_info,sizeof(query_info),0);//接收到查询的所有信息
	
	}

	if(flags == 0)//超级用户查询
	{
		send(socketfd, &info, sizeof(info), 0);//send name
		
		recv(socketfd, &info, sizeof(info), 0);//接收到查询姓名的等级
		name_query_level = info.level;
		
		recv(socketfd,&query_info,sizeof(query_info),0);//接收到查询的所有信息	
	}

		recv(socketfd, user, sizeof(USER), 0); // found?
		if(user->err){
			printf("not find!please check\n");
			return;
		}else{
			
			if(flags == 0)
			{
#if 1
				printf("name \t");printf("no  \t");printf("age   \t");
				printf("level \t");printf("phone \t \t");printf("addr \t");printf("salary     \n");
		
				printf("%-4s\t",query_info.name);
				printf("%-4s\t",query_info.no);
				printf("%-4s\t",query_info.age);
				printf("%-4s\t",query_info.level);
				printf("%-4s\t",query_info.phone);
				printf("%-4s\t",query_info.addr);
				printf("%-4s\n",query_info.salary);	
#endif
			}
			else
			{
				if(name_query_level == 2)
				{
					printf("no privilege\n");
				}
				else if(name_query_level == 1){

					printf("name \t");
					printf("phone  \t");
					printf("addr  \t");	
					printf("%-4s\t",query_info.name);
					printf("%-8s\t",query_info.phone);
					printf("%-4s\t",query_info.addr);
				
				}
				else{

				printf("name \t");printf("no  \t");printf("age   \t");
				printf("level \t");printf("phone   \t");printf("addr  \t");printf("salary     \n");
		
				printf("%-4s\t",query_info.name);
				printf("%-4s\t",query_info.no);
				printf("%-4s\t",query_info.age);
				printf("%-4s\t",query_info.level);
				printf("%-8s\t",query_info.phone);
				printf("%-4s\t",query_info.addr);
				printf("%-4s\n",query_info.salary);	
				}
			
			}
		}


	return;	
}
void do_admin_update(int socketfd, USER *user)
{
	INFO info;
	char ch = 0;
	
	memset(user, 0, sizeof(USER));
	
	switch(flags){
		case 0:
			user->type = ROOT_ALTER;//超级用户修改
			break;
		case 1:
			user->type = ADMIN_ALTER;//管理员更改
			break;
	}
	send(socketfd, user, sizeof(USER), 0);
	

	printf("Enter the name you want to change:");
	scanf("%s",user->name);
	
	printf("Do you want change password?(y/n)\n");
	getchar();
	scanf("%c",&ch);
	if (ch == 'y') {
		printf("input your new password:");
		scanf("%s",user->password);
	} else {
		user->password[0] = 0;
	}
	send(socketfd, user, sizeof(USER), 0);
	
	printf("input the name you want to update:");
	scanf("%s",info.name);	
	getchar();

	printf("input new addr:");
	scanf("%s",info.addr);
	getchar();

	printf("input new age:");
	scanf("%d",&info.age);
	getchar();
	
	printf("input new level:");
	scanf("%d",&info.level);
	getchar();
	
	printf("input new no:");
	scanf("%d",&info.no);
	getchar();

	printf("input newr phone:");
	scanf("%s",info.phone);	
	getchar();

	printf("input new salary:");
	scanf("%lf",&info.salary);	
	getchar();

	send(socketfd, &info, sizeof(INFO), 0);
	
	recv(socketfd, user, sizeof(USER), 0);
	if (user->err < 0) {
		printf("Change is error\n");
	} else {
		printf("Change is succeed\n");
	}
	
	return;
}
	
void do_admin_adduser(int socketfd, USER *user)
{
	INFO info;
	
	printf("input your name:");//添加登录账户
	scanf("%s",user->name);
	getchar();
	printf("input password:");
	scanf("%s",user->password);
	getchar();
	
	strcpy(info.name,user->name);//添加账户信息

	printf("input addr:");
	scanf("%s",info.addr);//地址

	getchar();
	printf("input age:");  //年龄
	scanf("%d",&info.age);
	getchar();
	printf("input staff level:");//等级
	scanf("%d",&info.level);
	getchar();
	printf("input staff number:");//员工id
	scanf("%d",&info.no);
	getchar();

	printf("input staff telphone:");//电话
	scanf("%s",info.phone);
	getchar();
	printf("input staff salary:");//工资
	scanf("%lf",&info.salary);
	getchar();
	
	switch(flags){
		case 0:
			user->type = ROOT_ADD_ADMIN;//超级用户添加
			break;
		case 1:
			user->type = ADMIN_ADD_USER;//管理员添加
			break;
	}
	
	send(socketfd, user, sizeof(USER), 0);
	send(socketfd, &info, sizeof(INFO), 0);
	recv(socketfd, user, sizeof(USER), 0);
	
	if(user->err){
		printf("addstaff failed\n");
	}else{
		printf("success\n");
	}
		
	return ;	
}	

void do_admin_deluser(int socketfd, USER *user)
{
	printf("input your delete name:");
	scanf("%s",user->name);

	
	
	switch(flags){
		case 0:
			user->type = ROOT_DEL_ADMIN;//超级用户删除
			break;
		case 1:
			user->type = ADMIN_DEL;//管理员删除
			break;
	}
	
	send(socketfd, user, sizeof(USER), 0);
	recv(socketfd, user, sizeof(USER), 0);
	
	if(user->err){
		printf("username is error,please check\n");
	}else{
		printf("success\n");
	}
}


int do_user_login(int socketfd, USER *user)
{
	user->type = USER_LOG;
	printf("input your name:");
	scanf("%s",user->name);
	printf("input your password:");
	scanf("%s",user->password);

	send(socketfd, user, sizeof(USER), 0);
	recv(socketfd, user, sizeof(USER), 0);
	//printf("user.err=%d\n",user->err);

	if(user->err){
		printf("username or password is error,please check\n");
		return -1;
	}else{
		printf("login success !\n");
	}
	
	return 0;
}

void do_query_and_update_myself(int socketfd, USER *user)
{
	int n;
	printf("***********************************************\n");
	printf("***********1:query  2:update  3:exit***********\n");
	printf("***********************************************\n");

	printf("please choose :");
	if(scanf("%d", &n) <= 0){
		perror("scanf");
		exit(-1);
	}
	switch(n)
	{
		case 1:
			do_query_myself(socketfd, user);
			break;
		case 2:
			do_update_myself(socketfd, user);
			break;	
		case 3:
			return;	//返回上一级
	}		
}

void do_update_myself(int socketfd, USER *user)
{
	INFO info;
	int ret = -1;
	char ch = 0;
	
	user->type = USER_ALTER;
	printf("Enter the name you want to change:");
	scanf("%s",user->name);
	send(socketfd, user, sizeof(USER), 0);
	
	printf("Do you want change password?(y/n)\n");
	getchar();
	scanf("%c",&ch);
	if (ch == 'y') {
		printf("input your new password:");
		scanf("%s",user->password);
	} else {
		user->password[0] = 0;
	}
	send(socketfd, user, sizeof(USER), 0);
	
	printf("input your new name:");
	scanf("%s",info.name);
	
	printf("input your addr:");
	scanf("%s",info.addr);
	
	getchar();
	printf("input your age:");
	scanf("%d",&info.age);
	
	printf("input your phone:");
	scanf("%s",info.phone);
	
	send(socketfd, &info, sizeof(INFO), 0);
	
	recv(socketfd, user, sizeof(USER), 0);
	if (user->err < 0) {
		printf("Change is error\n");
	} else {
		printf("Change is succeed\n");
	}
}	

void do_query_myself(int socketfd, USER *user)
{
	char buf[30];
	int name_query_level = -1;
	INFO info;
	query_desc_t query_info;


	printf("input query name:");
	scanf("%s",info.name);
	getchar();

	strcpy(buf,info.name);

	user->type = USER_QUERY;

	send(socketfd, user, sizeof(USER), 0);//发送查询信息，用户自己查询自己

	send(socketfd, &info, sizeof(info), 0);//发送要查询自己的名字

	recv(socketfd, &info, sizeof(info), 0);//接收查询名字的等级
	name_query_level = info.level;

	recv(socketfd,&query_info,sizeof(query_info),0);//接收到查询的所有信息
		

	recv(socketfd, user, sizeof(USER), 0); // found?
		if(user->err){
			printf("not find!please check\n");
			return;
	
		}else{

		

			printf("name \t");printf("no  \t");printf("age   \t");
			printf("level \t");printf("phone      \t");printf("addr   \t");printf("salary     \n");
		
			printf("%-4s\t",query_info.name);
			printf("%-4s\t",query_info.no);
			printf("%-4s\t",query_info.age);
			printf("%-4s\t",query_info.level);
			printf("%-8s\t",query_info.phone);
			printf("%-8s\t",query_info.addr);
			printf("%-4s\n",query_info.salary);			
	
	}
}

	
void do_query_others(int socketfd, USER *user)
{
	int name_query_level = -1;
	INFO info;
	query_desc_t query_info;


	printf("input query name:");
	scanf("%s",info.name);

	user->type = USER_QUERY_OTHER;

	send(socketfd, user, sizeof(USER), 0);//发送查询信息，用户自己查询自己


	send(socketfd, &info, sizeof(info), 0);//发送要查询自己的名字

	recv(socketfd, &info, sizeof(info), 0);//接收查询名字的等级
	name_query_level = info.level;

	recv(socketfd,&query_info,sizeof(query_info),0);//接收到查询的所有信息
		

	recv(socketfd, user, sizeof(USER), 0); // found?
		if(user->err){
			printf("not find!please check\n");
			return;
	
		}else{
				if(name_query_level > 0)
				{
					printf("no privilege\n");
					return;

				}
				else{

				printf("name \t");
				printf("phone      \t");
				printf("addr      \n");	
				printf("%-4s\t",query_info.name);
				printf("%-8s\t",query_info.phone);
				printf("%-8s\n",query_info.addr);

				}
			}			
}

